/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.perfectday.dashboard.gui.quests;

import es.bitsonfire.PDMinisDatabase;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.log4j.Logger;
import org.perfectday.logicengine.core.configuration.Configuration;
import org.perfectday.oneplayer.battleloaders.LoaderOfQuest;
import org.perfectday.oneplayer.battleloaders.exception.LoadQuestException;
import org.perfectday.oneplayer.battleloaders.model.Quest;

/**
 * Implementamos a modo de men? selector de misiones (quests) compuesto por un
 * selector en la parte izquierda y la representaci?n de la informaci?n en el
 * centro.
 *
 * @author Miguel (alakat@gmail.com)
 */
public class SelectQuestPanel extends javax.swing.JPanel implements ListSelectionListener {

    private Logger logger = Logger.getLogger(SelectQuestPanel.class);
    private Quest selectedQuest;
    private List<String> quests;

    /*
     Me siento sucio por la innerclass....
     */
    private ListModel<String> listModelQuest = new ListModel<String>() {

        @Override
        public int getSize() {
            return quests.size();
        }

        @Override
        public String getElementAt(int index) {
            return quests.get(index);
        }

        @Override
        public void addListDataListener(ListDataListener l) {

        }

        @Override
        public void removeListDataListener(ListDataListener l) {

        }
    };

    /**
     * Creates new form SelectQuestPanel
     */
    public SelectQuestPanel() {
        initComponents();
        this.quests = new ArrayList<String>();
        this.quests.addAll(LoaderOfQuest.getInstance().getQuest());
        this.missionSelected.setModel(this.listModelQuest);
        this.missionSelected.addListSelectionListener(this);
        this.showQuest(this.quests.get(0));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        missionSelected = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        missionIcon = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        missionDescription = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        missionTitle = new javax.swing.JLabel();

        missionSelected.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(missionSelected);

        missionDescription.setEditable(false);
        missionDescription.setBackground(new java.awt.Color(204, 204, 204));
        missionDescription.setColumns(20);
        missionDescription.setRows(5);
        jScrollPane2.setViewportView(missionDescription);

        jPanel2.setLayout(new java.awt.BorderLayout());

        missionTitle.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
        missionTitle.setText("missions");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(369, 369, 369)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(missionIcon, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(missionTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(missionTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                    .addComponent(missionIcon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea missionDescription;
    private javax.swing.JLabel missionIcon;
    private javax.swing.JList missionSelected;
    private javax.swing.JLabel missionTitle;
    // End of variables declaration//GEN-END:variables

    public Quest getSelectedQuest() {
        return this.selectedQuest;
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        logger.debug("Nuevo evento de la lista:" + e);
        this.newQuestSelected();
    }

    /**
     *
     */
    private void newQuestSelected() {

        int newIndex = this.missionSelected.getSelectedIndex();
        String quest =(String) this.missionSelected.getSelectedValue();
        showQuest( quest);

    }

    public void showQuest(String quest_) {
        try {

            Quest quest = LoaderOfQuest.getInstance().getQuest(quest_);
            logger.debug("Show Quest[]:"+quest);
            this.selectedQuest = quest;
            this.missionTitle.setText(quest.getTitle());
            this.missionDescription.setText(quest.getDescription());
            logger.debug("Loading icon:"+Configuration.QUEST_PATH+
                            Configuration.QUEST_TOKEN_SEPARATOR+quest.getQuest()+"/icon_"+quest.getQuest()+".jpeg");
            URL url =  PDMinisDatabase.class.getResource(Configuration.QUEST_PATH+
                            Configuration.QUEST_TOKEN_SEPARATOR+quest.getQuest()+"/icon_"+quest.getQuest()+".jpeg");
            logger.debug(
            PDMinisDatabase.class.getResourceAsStream(Configuration.QUEST_PATH+
                            Configuration.QUEST_TOKEN_SEPARATOR+quest.getQuest()+"/icon_"+quest.getQuest()+".jpeg")==null);
            Icon icon = new ImageIcon(url);
            this.missionIcon.setIcon(icon);
        } catch (IOException ex) {
            JOptionPane.showConfirmDialog(null,
                    "Error al cargar la misi?n seleccionada",
                    "Error en misi?n",
                    JOptionPane.ERROR_MESSAGE);
            logger.error(ex.getMessage(), ex);
        } catch (LoadQuestException ex) {
            JOptionPane.showConfirmDialog(null,
                    "Error al cargar la misi?n seleccionada."
                    + " La descripci?n de la misi?n conten?a errores",
                    "Error en misi?n",
                    JOptionPane.ERROR_MESSAGE);
            logger.error(ex.getMessage(), ex);
        }
    }

}
